﻿using System;
using UnityEngine;

namespace WzhAddon
{
	public class ShaderUtil
	{
		private static Shader _TransparentZAlways;
		private static Shader _SpriteDefaultZAlways;
		private static Shader _AlphaVertexlit;
		public static Shader TransparentZAlways { get { return ShaderUtil._TransparentZAlways; } }
		public static Shader AlphaVertexLit { get { return ShaderUtil._AlphaVertexlit; } }
		public static Shader SpriteDefaultZAlways { get { return ShaderUtil._SpriteDefaultZAlways; } }
		static ShaderUtil()
		{
			ShaderUtil.LoadShaders();
		}

		private static void LoadShaders()
		{
			ShaderUtil.FindAndSetShader(Resources.FindObjectsOfTypeAll<Shader>());
			if (ShaderUtil._TransparentZAlways != null) return;
			try
			{
				AssetBundle assetBundle = AssetBundle.LoadFromFile("abdata/studio/materials/03.unity3d");
				assetBundle.LoadAllAssets();
				ShaderUtil.FindAndSetShader(Resources.FindObjectsOfTypeAll<Shader>());
				assetBundle.Unload(false);
			}
			catch (Exception value)
			{
				Console.WriteLine(value);
			}
		}

		private static void FindAndSetShader(Shader[] shaders)
		{
			foreach (Shader shader in shaders)
			{
				if (shader.name == "Unlit/Transparent") ShaderUtil._TransparentZAlways = shader;
				if (shader.name == "Custom/Unlit/UnlitTextureColorTransparent") ShaderUtil._SpriteDefaultZAlways = shader;
				else if (shader.name == "Custom/AdditiveTransparentColor") ShaderUtil._AlphaVertexlit = shader;
				if (ShaderUtil._TransparentZAlways != null && ShaderUtil._SpriteDefaultZAlways != null && ShaderUtil._AlphaVertexlit != null) return;
			}
		}
	
	}
}
